package com.alks.function.controller.pcfactory;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.pcfactorywork.OutUnitPriceDto;
import com.alks.function.data.request.factorywork.*;
import com.alks.function.service.pcfactorywork.IPcDhLotHeadService;
import com.alks.function.service.pcfactorywork.IPcSupplementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author MS
 * @since 2024/9/24上午11:07
 */
@RestController
@ApiRestController("/pcFactorWork")
@Api(value = "总经办", tags = {"总经办-工厂作业模块-外接单价审核","总经办-工厂查询模块-补单查询作业","成控部-成本作业-外接单价输入"})
@Slf4j
public class OutPriceChekController {
    @Autowired
    private IPcDhLotHeadService lService;
    @Autowired
    private IPcSupplementService sService;;


    @PutMapping("/listDhHead")
    @ApiOperation(value = "外接单价-指令资料-可条件分页查询", tags = "总经办-工厂作业模块-外接单价审核")
    @WebLogger
    public ResponseInfo listPcDhLot(@RequestBody PcDhLotRequest request) {
        Map<String, Object> dto = lService.listPcDhLot(request);
        return ResponseInfo.ok(dto);
    }

    @GetMapping("/listDhDetail/{lotNo}")
    @ApiOperation(value = "外接单价-预算明细-条件查询", tags = "总经办-工厂作业模块-外接单价审核")
    @WebLogger
    public ResponseInfo listPcDhPrice(@PathVariable(value = "lotNo") String lotNo) {
        return lService.listPcDhPrice(lotNo);
    }

    @PutMapping("/auditDhDetail")
    @ApiOperation(value = "外接单价-指令资料-批量审核", tags = "总经办-工厂作业模块-外接单价审核")
    @WebLogger
    public ResponseInfo auditDhDetail(@RequestBody PcDhLotCFMRequest request) {
        return lService.auditDhDetail(request);
    }


    @PostMapping("/listPcDhPriceExcel")
    @ApiOperation(value = "外接单价-预算明细-导出明细（Excel）",tags = "总经办-工厂作业模块-外接单价审核")
    @WebLogger
    public ResponseInfo listPcDhPriceExcel(@RequestParam@ApiParam("指令") String lotNo, HttpServletResponse response){
        Map<String, Object> data = lService.listPcDhPrice(lotNo).getData();
        String templetName = "预算费用明细模版";
        String excelName = "预算费用明细"+ DateUtils.dateTimeNow();
        try {
            ExcelUtils.creatByTemplate(response,templetName,excelName, (List<?>) data.get("dtos"),data);
        } catch (IOException e) {
            return ResponseInfo.error("导出失败");
        }
        return null;
    }

    @ApiOperation(value = "补料单资料", tags = "总经办-工厂查询模块-补单查询作业")
    @PostMapping("/getSupplementaryOrder")
    @WebLogger
    public ResponseInfo getSupplementaryOrder(@RequestBody SupplementaryOrderRequest request) {
        return sService.SupplementaryOrders(request);
    }

    @GetMapping("/getSupplementaryDetail")
    @ApiOperation(value = "补制明细", tags = "总经办-工厂查询模块-补单查询作业")
    @WebLogger
    public ResponseInfo getSupplementaryDetail(@RequestParam String slipNo) {
        return sService.getSupplementaryDetails(slipNo);
    }

    @ApiOperation(value = "条件返回", tags = "总经办-工厂查询模块-补单查询作业")
    @GetMapping("/getDept2Type")
    @WebLogger
    public ResponseInfo getDept2Type() {
        return sService.getDept2Type();
    }

    @ApiOperation(value = "插入外接单价", tags = "成控部-成本作业-外接单价输入")
    @PutMapping("/InsertExternalInitPrice")
    @WebLogger
    public ResponseInfo InsertExternalInitPrice(@RequestBody PcLotPriceOtherUpdateRequest request) {
        return lService.InsertExternalInitPrice(request);
    }

    @ApiOperation(value = "预算项目新增", tags = "成控部-成本作业-外接单价输入")
    @PostMapping("/AddBudgetItem")
    @WebLogger
    public ResponseInfo AddBudgetItem(@RequestBody PcLotPriceOtherAddRequest request) {
        return lService.AddBudgetItem(request);
    }

    @ApiOperation(value = "预算项目修改", tags = "成控部-成本作业-外接单价输入")
    @PutMapping("/ModifyBudgetItems")
    @WebLogger
    public ResponseInfo ModifyBudgetItems(@RequestBody PcLotPriceOtherAddRequest request) {
        return lService.ModifyBudgetItems(request);
    }

    @ApiOperation(value = "预算项目删除", tags = "成控部-成本作业-外接单价输入")
    @PutMapping("/DeleteBudgetItems")
    @WebLogger
    public ResponseInfo DeleteBudgetItems(@RequestBody PcLotPriceOtherAddRequest request) {
        return lService.DeleteBudgetItems(request);
    }

    @ApiOperation(value = "导出", tags = "总经办-工厂作业模块-外接单价审核")
    @PutMapping("/export")
    @WebLogger
    public void export(@RequestBody PcDhLotRequest request,HttpServletResponse response) {
        String templetName = "外接单价审核模版.xlsx";
        String excelName = "外接单价审核"+ DateUtils.dateTimeNow();
        request.setPageNum(-1);
        request.setPageSize(-1);
        Map<String, Object> map = lService.listPcDhLot(request);
        try {
            ExcelUtils.creatByTemplate(response,templetName,excelName, (List<OutUnitPriceDto>) map.get("dtos"),map);
        } catch (IOException e) {
            throw new ServiceErrorException("导出失败");
        }
    }
}